<?php
    if(isset($_ECLIPSMDE) === false) {
	$_ECLIPSEMDE = array();
    }
    $_ECLIPSEMDE['ROOT_WD'] = realpath(__DIR__).'/';
    if(isset($_SERVER['SERVER_NAME']) === true && isset($_SERVER['SERVER_PORT']) === true) {
	$_ECLIPSEMDE['BASE_URL'] = 'http://'.$_SERVER['SERVER_NAME'];
	if($_SERVER['SERVER_PORT'] != "80") {
	    $_ECLIPSEMDE['BASE_URL'] .= ':'.$_SERVER['SERVER_PORT'];
	}
    } else {
	$_ECLIPSEMDE['BASE_URL'] = '';
    }
    $_ECLIPSEMDE['RSS_PATH'] = $_ECLIPSEMDE['ROOT_WD'].'rss-feeds/';
    define('___SIMPLEPIE_CACHEDIR___', $_ECLIPSEMDE['ROOT_WD'].'temp/simplepie_cache');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/utils/PageTimer.php');
    
    
    define('___TEMPLATINGENGINE___', 'BootStrapTemplate');
    require_once($_ECLIPSEMDE['ROOT_WD'].'fwork/incl_fwork.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/utils/StashMap.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/utils/EclipseMDE.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/user/Password.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/project/ProjectStash.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/tags/TagStash.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/user/UserStash.php');
    require_once($_ECLIPSEMDE['ROOT_WD'].'core/user/UserSession.php');
    /**
     * Call this function on a page you require SimplePie and its Aggregation system.
     * Not having it everywhere triples the execution speed.
     */
    function require_simplepie() {
	global $_ECLIPSEMDE;
	require_once($_ECLIPSEMDE['ROOT_WD'].'core/utils/SimplePie.php');
	require_once($_ECLIPSEMDE['ROOT_WD'].'core/utils/SimplePieAggregator.php');
    }
    
    try {
	/*
	 * We are trying to instancate the $_LOGGEDINSESSION variable and setup $_LOGGEDINUSER
	 * $_LOGGEDINUSER	=> Holds the User Instance of Class 'User' that we can use anywhere
	 * $_LOGGEDINSESSION	=> Holds the User Session Instance of Class 'UserSession' that we can use anywhere
	 * 
	 * if no user is logged in:
	 * $_LOGGEDINUSER	=> FALSE
	 * $_LOGGEDINSESSION	=> null	    ( isset($_LOGGEDINSESSION) === false )
	 */
	$_LOGGEDINUSER = false;
	if(isset($_COOKIE['sessx']) === true) {
	    // Create Session
	    $_LOGGEDINSESSION = new UserSession();
	    // Setup the session based on the users cookie $_COOKIE['sessx'], if set
	    if(isset($_COOKIE['sessx']) === true && $_LOGGEDINSESSION->verifySession($_COOKIE['sessx']) === true) {
		$_LOGGEDINUSER =& $UserStash->get((int)$_LOGGEDINSESSION->getUserID());
		// Logout if need to
		
		/**
		 * This switch handles log out if we are on /su/logout 
		 */
		if(isset($_SERVER['REDIRECT_URL']) && $_SERVER['REDIRECT_URL'] == '/su/logout') {
		    $_LOGGEDINSESSION->destroySession();
		    if(isset($_LOGGEDINSESSION) === true)
			unset($_LOGGEDINSESSION);
		    unset($_LOGGEDINUSER); $_LOGGEDINUSER = false;
		}
	    } else {
		/**
		 * Either $_COOKIE['sessx'] is not set OR the session is invalid (expired for example). 
		 * DO NOT CALL THE DESTRUCTOR METHOD OF $_LOGGEDINSESSION WHEN USING unset.
		 */
		unset($_LOGGEDINSESSION);
	    }
	}
    } catch(InvalidUserException $e) {
	/*
	 * For whatever reason the user could not be found...
	 * If its got to this point, we have had a stash attempt of (say) a none-existant ID - hack attempt?
	 * Lets destroy all session data and instruct their browser to invalidate their cookie. (will have been done in verifySession).
	 * After this, User is considered Guest
	 */
	$_LOGGEDINUSER = null; unset($_LOGGEDINUSER);
    } catch(Exception $e) {
	/**
	 * This is a "should-never-happen". 
	 * We should stop execution.
	 * This will cause the backup 500 error in libDebug.
	 */
	throw new RuntimeException($e);
    }
    
	/* To Reduce Overheads, we can leave out the templating system if this is a
	 * non-templating response. (For Example, AJAX).
	 */
    if(isset($_ECLIPSEMDE['TEMPLATING']) === false || (isset($_ECLIPSEMDE['TEMPLATING']) === true && $_ECLIPSEMDE['TEMPLATING'] === true)) {
	require_once($_ECLIPSEMDE['ROOT_WD'].'core/template/BootStrapTemplate.php');
	require_once($_ECLIPSEMDE['ROOT_WD'].'core/template/TemplateE.php');  
    
	$Templater = $TemplateEngine->getTemplater();
	
	// Calls in the below files may DEPEND on $_LOGGEDINUSER.
	require_once($_ECLIPSEMDE['ROOT_WD'].'sitewide_template_includes/NavigationBar.php');
	require_once($_ECLIPSEMDE['ROOT_WD'].'sitewide_template_includes/UserDropDown.php');
    }
    
    
    /*
     * These had to go somewhere... here seemed logical enough.
     * They are the very basic OK/NOK Calls to the parent page from an AJAX Page.
     */
    $nok_caller = <<<EOJS
	<!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
	    <script type="text/javascript">parent.EDJXConfirmer.show_nok();</script>
	    </body>
	</html>
EOJS;
    $ok_caller = <<<EOJS
	<!DOCTYPE html>
	    <html>
	    <head><title></title></head>
	    <body>
	    <script type="text/javascript">parent.EDJXConfirmer.show_ok();</script>
	    </body>
	</html>
EOJS;
    
?>
